package com.shanghaiyimu.yimiyangche.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.MyAdapter;
import com.shanghaiyimu.yimiyangche.bean.PersionInfo;
import com.shanghaiyimu.yimiyangche.fragment.MyFragment;

import java.util.ArrayList;
import java.util.List;

public class CarGoodsActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    List<PersionInfo> listinfoInfos=new ArrayList<PersionInfo>();
    private ListView listView;
    private MyAdapter adapter;
    private MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_goods);
        initView();
    }
    /**
     * 初始化view
     */
    private void initView() {
//        for (int i = 0; i < 10; i++) {
//            PersionInfo info=new PersionInfo("商品分类"+i);
//            listinfoInfos.add(info);
//    }
        PersionInfo info=new PersionInfo("汽保工具");
        listinfoInfos.add(info);
        PersionInfo info1=new PersionInfo("汽车用品");
        listinfoInfos.add(info1);
        PersionInfo info2=new PersionInfo("美容清洗");
        listinfoInfos.add(info2);
        PersionInfo info3=new PersionInfo("外饰改装");
        listinfoInfos.add(info3);
        PersionInfo info4=new PersionInfo("安全自驾");
        listinfoInfos.add(info4);
        PersionInfo info5=new PersionInfo("车载电器");
        listinfoInfos.add(info5);
        PersionInfo info6=new PersionInfo("汽车装饰");
        listinfoInfos.add(info6);
        PersionInfo info7=new PersionInfo("新车销售");
        listinfoInfos.add(info7);
        //
        listView = (ListView) findViewById(R.id.listview);
        //默认
        listinfoInfos.get(0).setChick(true);
        adapter = new MyAdapter(this, listinfoInfos);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);

        //创建MyFragment对象
        myFragment = new MyFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, myFragment);

        Bundle mBundle=new Bundle();
        mBundle.putSerializable("info", listinfoInfos.get(0));
        myFragment.setArguments(mBundle);
        fragmentTransaction.commit();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {

        PersionInfo info = listinfoInfos.get(position);
        for (int i = 0; i < listinfoInfos.size(); i++) {
            if(listinfoInfos.get(i).getNameString().equals(info.getNameString())){
                listinfoInfos.get(i).setChick(true);
            }else {
                listinfoInfos.get(i).setChick(false);
            }
        }

        adapter.notifyDataSetChanged();


        //
        //创建MyFragment对象
        myFragment = new MyFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, myFragment);

        Bundle mBundle=new Bundle();
        mBundle.putSerializable("info", listinfoInfos.get(position));
        myFragment.setArguments(mBundle);
        fragmentTransaction.commit();
    }
}



